<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <span class="el-laber">请选择商品分类:</span>
          <el-cascader
            v-model="selectValue"
            :options="CateDateList"
            placeholder="请选择分类"
            :props="{ value: 'cat_id', label: 'cat_name' }"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="1">动态参数</el-menu-item>
        <el-menu-item index="2">静态参数</el-menu-item>
      </el-menu>

      <el-button style="margin:20px 0;" size="mini" type="primary" @click="handleAddGraphy">添加参数</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="attr_name" label="名称" width="380"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleGraphyMessage(scope.row)"
              style="margin-right:6px;"
            >编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteGraphy(scope.row.attr_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加版型 -->
    <el-dialog :title="graphyTitle" v-model="dialogTableVisible" width="700px">
      <div class="dialog-content">
        <span class="el-laber">动态参数:</span>
        <el-input placeholder="请填写版型" v-model="graphyVal"></el-input>
      </div>
      <span class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleDoGraphy">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, toRefs, ref, onMounted } from 'vue'
import {
  GET_GOODSCATE,
  GET_GRAPHYMESSAGE,
  ADD_GRAPHYMESSAGE,
  HANDLE_GRAPHYMESSAGE,
  DELETE_GRAPHYMESSAGE,
} from '../../api/request'
import { ElMessage, ElMessageBox } from 'element-plus'
import { createLogger } from 'vuex'
export default {
  setup() {
    const state = reactive({
      selectValue: '',
      activeIndex: '1', //菜单选中状态
      CateDateList: [], //分类列表
      props: { expandTrigger: 'hover' },
      tableData: [],
      graphyId: '',
      editFromData: {}, //当前修改的分类对象
      graphyTitle: '添加版型',
      dialogTableVisible: false, //对话框
    })
    onMounted(() => {
      getGoodsCate()
    })
    const handleSelect = (index) => {
      console.log(index)
      state.activeIndex = index
    }
    // 获取商品分类列表
    const getGoodsCate = async () => {
      const { data } = await GET_GOODSCATE()
      state.CateDateList = data
    }
    const handleChange = (value) => {
      state.graphyId = value.join('')
      getTableList(state.graphyId)
    }
    // 获取表格内的数据
    const getTableList = (id) => {
      GET_GRAPHYMESSAGE(id).then((res) => {
        state.tableData = res.data
      })
    }
    // 更新版型名称
    const handleGraphyMessage = (row) => {
      // 存储当前操作的对象
      state.editFromData = row
      graphyVal.value = row.attr_name
      state.graphyTitle = '编辑版型'
      state.dialogTableVisible = true
    }
    // 打开对话框 添加版型
    const handleAddGraphy = () => {
      // 判断是否已选择分类
      if (!state.graphyId) {
        ElMessage.warning('请先选择分类')
        return
      }
      state.graphyTitle == '添加版型'
      graphyVal.value = ''
      state.dialogTableVisible = true
    }
    // 确认添加版型
    const graphyVal = ref('')
    const handleDoGraphy = () => {
      state.dialogTableVisible = false
      // 获取输入框的内容
      const value = graphyVal.value
      const data = {
        attr_name: value,
        attr_sel: 'many',
      }
      if (state.graphyTitle == '编辑版型') {
        const {
          graphyId,
          editFromData: { attr_id },
        } = state
        HANDLE_GRAPHYMESSAGE(graphyId, attr_id, data).then((res) => {
          if (res.meta.status == 200) {
            ElMessage.success('修改成功')
            getTableList(state.graphyId)
          } else ElMessage.error('修改失败')
          getTableList(state.graphyId)
        })
      } else {
        ADD_GRAPHYMESSAGE(state.graphyId, data).then((res) => {
          if (res.meta.status == 201) {
            ElMessage.success('添加成功')
            getTableList(state.graphyId)
          } else ElMessage.error('添加失败')
        })
      }
    }
    // 删除版型
    const handleDeleteGraphy = (id) => {
      ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          DELETE_GRAPHYMESSAGE(state.graphyId, id).then((res) => {
            setTimeout(() => {
              ElMessage.success('删除成功!')
            }, 300)
            getTableList(state.graphyId)
          })
        })
        .catch(() => {
          ElMessage.error('已取消删除')
        })
    }
    return {
      ...toRefs(state),
      graphyVal,
      handleChange,
      handleSelect,
      handleDoGraphy,
      handleAddGraphy,
      handleGraphyMessage,
      handleDeleteGraphy,
    }
  },
}
</script>

<style scoped lang="scss">
.el-laber {
  margin-right: 6px;
}
.dialog-content {
  display: flex;
  align-items: center;
  padding: 40px 0;
  .el-input {
    width: 80%;
  }
}
</style>
